package com.example.ar_10.myobj;

import android.app.Activity;
import android.content.Context;

import com.example.ar_10.MyCamera;
import com.example.ar_10.MainActivity;
import com.example.ar_10.base.MatrixState;

/**
 * GL参数保存
 */
public class PositionData {

    private static final String TAG = PositionData.class.getSimpleName();
    private static Context contxt;
    private static MainActivity activity;
    private static float[] xyzCamera = new float[3];
    private static float[] xyzModel = new float[3];
    private static final MyCamera myCamera = new MyCamera();
    public static float[] mvMatrix = new float[3];

    public PositionData() {

    }

    public static float[] getMvMatrix() {
        return activity.getModel_view_matrix();
    }

    //near far
    public float PROJECTION_NEAR = 0.01f;
    public float PROJECTION_FAR = 10000;

    //camera position
    public static final float EYE_X = 0;
    public static final float EYE_Y = 0;
    public static final float EYE_Z = 0;
    public static final float VIEW_CENTER_X = 0;
    public static final float VIEW_CENTER_Y = 0;
    public static final float VIEW_CENTER_Z = 0;
    private static MatrixState matrixState;


    public Context getContxt() {
        return contxt;
    }

    public void setContxt(Context contxt) {
        this.contxt = (MainActivity) contxt;
    }

    public Activity getActivity() {
        return (MainActivity) activity;
    }

    public void setActivity(Activity activity) {
        this.activity = (MainActivity) activity;
    }

    //将MainActivity传递到类中使用
    public PositionData(Context context, MainActivity activity) {
        this.setContxt(context);
        this.setActivity(activity);
    }


    //获取经纬度
    public static float getLat() {
        return activity.getCurrentLat();
    }

    public static float getLon() {
        return activity.getCurrentLon();
    }

    public static float getAlt() {
        return activity.getCurrentAlt();
    }

    //获取手机姿态
    public static float getPitch() {
        return activity.getCurrentPitch();
    }

    public static float getRoll() {
        return activity.getCurrentRoll();
    }

    public static float getAzimuth() {
        return activity.getCurrentAzimuth();
    }


    //将经纬度转换为高斯投影坐标
    public static float[] LLAtoECEF(float B, float L, float H) {
        double a = 6378137;
        double e = Math.sqrt(0.0066943799013);
        double scale_wide = 6;
        double π = 3.14159265358979323846;
        double A_ = 1
                + 3 * e * e / 4
                + 45 * e * e * e * e / 64
                + 175 * e * e * e * e * e * e / 256
                + 11025 * e * e * e * e * e * e * e * e / 16384
                + 43659 * e * e * e * e * e * e * e * e * e * e / 65536;
        double B_ = 3 * e * e / 4 + 15 * e * e * e * e / 16
                + 525 * e * e * e * e * e * e / 512
                + 2205 * e * e * e * e * e * e * e * e / 2048
                + 72765 * e * e * e * e * e * e * e * e * e * e / 65536;
        double C_ = 15 * e * e * e * e / 64
                + 105 * e * e * e * e * e * e / 256
                + 2205 * e * e * e * e * e * e * e * e / 4096
                + 10395 * e * e * e * e * e * e * e * e * e * e / 16384;
        double D_ = 35 * e * e * e * e * e * e / 512
                + 315 * e * e * e * e * e * e * e * e / 2048
                + 31185 * e * e * e * e * e * e * e * e * e * e / 13072;

        /*
		double E_ =  315*e*e*e*e*e*e*e*e/16384
 				+3465*e*e*e*e*e*e*e*e*e*e/65536;
 		double F_ =  693*e*e*e*e*e*e*e*e*e*e/13072;
 		*/

        double α = A_ * a * (1 - e * e);
        double β = -B_ * a * (1 - e * e) / 2;
        double γ = C_ * a * (1 - e * e) / 4;
        double δ = -D_ * a * (1 - e * e) / 6;

        /*
 		double ε  =  E_*a*(1-e*e)/8;
 		double ζ  = -F_*a*(1-e*e)/10;
 		*/

        double C0 = α;
        double C1 = 2 * β + 4 * γ + 6 * δ;
        double C2 = -8 * γ - 32 * δ;
        double C3 = 32 * δ;
        double x, y, sign;
        double scale_number = Math.floor(L / scale_wide);
        if (L > (scale_number * scale_wide + scale_wide / 2)) {
            scale_number = scale_number + 1;
            sign = 1;
        } else {
            sign = 1;
        }

        double L0 = scale_wide * scale_number - 3;
        double l = Math.abs(L - L0);
        B = (float) (B * π / 180);
        l = l * π / 180;
        double t = Math.tan(B);
        double m0 = Math.cos(B) * l;
        double η = Math.sqrt(e * e * Math.pow(Math.cos(B), 2) / (1 - e * e));
        double N = a / Math.sqrt(1 - e * e * Math.pow(Math.sin(B), 2));

        double X0 = C0 * B + Math.cos(B) * (C1 * Math.sin(B) + C2 * Math.pow(Math.sin(B), 3) + C3 * Math.pow(Math.sin(B), 5));

        x = X0
                + N * t * m0 * m0 / 2
                + N * t * m0 * m0 * m0 * m0 * (5 - t * t + 9 * η * η + 4 * η * η * η * η) / 24
                + N * t * m0 * m0 * m0 * m0 * m0 * m0 * (61 - 58 * t * t + t * t * t * t) / 720;
        y = N * m0 +
                N * m0 * m0 * m0 * (1 - t * t + η * η) / 6 +
                N * m0 * m0 * m0 * m0 * m0 * m0 * (5 - 18 * t * t + t * t * t * t + 14 * η * η - 58 * η * η * t * t) / 120;

        y = y * sign + 500000;

        double h = H;

        float[] xyz = new float[]{
                (float) y, (float) x, (float) h
        };
        return xyz;
    }

    //获取相机位置
    public static float[] xyzCamera() {
//        xyzCamera = LLAtoECEF(getLat(), getLon(), 0);
//        xyzCamera = myCamera.getModel_view_matrix();
        xyzCamera[0] = -activity.getModel_view_matrix()[12];
        xyzCamera[1] = -activity.getModel_view_matrix()[13];
        xyzCamera[2] = -activity.getModel_view_matrix()[14];
        return xyzCamera;
    }

    //设置模型位置
    public static float[] xyzModel() {
//        xyzModel = LLAtoECEF(29.489872f, 106.571754f, -10);
        xyzModel[0] = 0f;
        xyzModel[1] = 0f;
        xyzModel[2] = 0f;
        return xyzModel;
    }

    public static float[] getXyzCamera() {
        return xyzCamera;
    }

    public static float[] getXyzModel() {
        return xyzModel;
    }

}
